How to remove items from an arraylist without shrinking the list [migrated]

Posted by user73710 on Programmers See other posts from Programmers or by user73710
Published on 2012-11-27T02:44:29Z Indexed on 2012/11/27 5:28 UTC
Read the original article Hit count: 139

Filed under:

I have a case where I am using the ArrayList to keep a list of items that are keyed by their position in the list. Other objects reference the ArrayList items by their position. If I delete one of the items from the list, I don't want the list to shrink because that would invalidate all other references to items in the list (e.g. item 2 is now in position 1). My solution to the shrinking array list problem is to null the position in the arraylist so that the list will not shrink. I am curious whether this will free the memory formerly held by the item at that position.

If there is a better way to accomplish this requirement, I would like to know about it.

© Programmers or respective owner

Related posts about java